import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import spider.dao.DictBrand;
import spider.dao.DictModel;
import spider.utils.hibernate.DataManage;
import spider.utils.html.HtmlContentParser;
import spider.utils.http.HttpGetConnect;
/**
 * 解析网页中的数据
 */
public class TestHtml2 {

	private static int globalIndex = 1;
	private static Map<String,Integer> brandMap = new HashMap<>();
	
    public static void main(String[] args) throws Exception { 
    	for(int i = 0;i<26;i++){
    		String index = (char)('A'+i)+"";
    		parseURL( "http://www.autohome.com.cn/grade/carhtml/"+index+".html",index);
    	}
        
    }

	private static void parseURL(String url,String py)
			throws URISyntaxException, IOException {
		
		System.out.println("==================="+py+"=======================");
		
		DataManage manage = new DataManage();    
		HttpGetConnect get = new HttpGetConnect();       
        String content = get.connect( url, "gbk");
        Document doc = Jsoup.parse(HtmlContentParser.clean(content));
        
        manage.session.beginTransaction();        
        Elements list =  doc.select("body>dl>dd");
        for(Element ele:list){
        	Elements titleList = ele.select(".h3-tit a");
        	for(int i = 0;i<titleList.size();i++){
        		
        		Element titleEle = titleList.get(i);
        		String title = titleEle.text();
        		
        		if(brandMap.get(title) != null){ //已经存在，忽略即可
        			continue;
        		}else{
        			brandMap.put(title, 1);
        		}
        		
        		
        		
        		DictBrand b = new DictBrand();
            	b.setName(title);
            	b.setDeleted(0);
            	b.setSortOrder(globalIndex++);
            	b.setPy(py);
            	manage.session.save(b);
            	
            	System.out.println(title);
            	
            	
            	//获取对应的型号            	
            	Elements brandList = ele.select(".rank-list-ul").get(i).select("li h4 a");
            	for(Element model :brandList){
            		DictModel m = new DictModel();
            		m.setBrand(b);
            		m.setName(model.text());
            		m.setDeleted(0);
            		manage.session.save(m);
            		
            		System.out.print(model.text());
            	}
            	System.out.println();
        	}
        	
        	
        
        	System.out.println("");
        }
        manage.session.getTransaction().commit();
	}

}
